<template>
  <div class="Roles">
    <!-- 面包屑 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片 -->
    <el-card class="box-card">
      <el-button type="primary" @click="duihuakuang2 = true"
        >添加角色</el-button
      >
      <el-table border :data="Roleslist" stripe>
        <el-table-column type="expand">
          <template v-slot:default="scope">
            <el-row
              :class="['bdbom', 'juzhong', index1 == 0 ? 'bdtop' : '']"
              v-for="(item1, index1) in scope.row.children"
              :key="item1.id"
            >
              <!-- 一级权限 -->
              <el-col :span="5">
                <el-tag closable @close="shanchu3(scope.row, item1.id)">
                  {{ item1.authName }}</el-tag
                >
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 二级权限 -->
              <el-col :span="19">
                <el-row
                  v-for="(item2, index2) in item1.children"
                  :key="item2.id"
                  :class="['juzhong', index2 == 0 ? '' : 'bdtop']"
                >
                  <el-col :span="6">
                    <el-tag
                      closable
                      @close="shanchu3(scope.row, item2.id)"
                      type="success"
                    >
                      {{ item2.authName }}</el-tag
                    >
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag
                      v-for="item3 in item2.children"
                      type="warning"
                      :key="item3.id"
                      closable
                      @close="shanchu3(scope.row, item3.id)"
                    >
                      {{ item3.authName }}
                    </el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <el-table-column label="#" type="index"></el-table-column>
        <el-table-column label="角色名称" prop="roleName"> </el-table-column>
        <el-table-column label="角色描述" prop="roleDesc"> </el-table-column>
        <el-table-column label="操作" prop="">
          <template v-slot:default="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="showEditDialog(scope.row.id)"
              >编辑</el-button
            >
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeUserById(scope.row.id)"
              >删除</el-button
            >
            <!-- 分配角色按钮 -->
            <el-tooltip
              effect="dark"
              content="分配角色"
              placement="top"
              :enterable="false"
            >
              <el-button
                type="warning"
                @click="fenpei(scope.row)"
                icon="el-icon-setting"
                size="mini"
              >
                分配角色<!--  @click="setRole(scope.row)" -->
              </el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <el-dialog
      @close="guanbi"
      title="修改角色"
      :visible.sync="duihuakuang"
      width="50%"
    >
      <!-- 内容主体区域 -->
      <el-form :model="usfrom" ref="usfromref" label-width="70px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="usfrom.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="usfrom.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="duihuakuang = false">取 消</el-button>
        <el-button type="primary" @click="queding">确 定</el-button>
      </span>
    </el-dialog>
    <el-dialog
      @close="guanbi2"
      title="添加角色"
      :visible.sync="duihuakuang2"
      width="50%"
    >
      <!-- 内容主体区域 -->
      <el-form :model="addfrom" ref="addfromref" label-width="70px">
        <el-form-item label="角色名称" prop="roleName">
          <el-input v-model="addfrom.roleName"></el-input>
        </el-form-item>
        <el-form-item label="角色描述" prop="roleDesc">
          <el-input v-model="addfrom.roleDesc"></el-input>
        </el-form-item>
      </el-form>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="duihuakuang2 = false">取 消</el-button>
        <el-button type="primary" @click="queding2">确 定</el-button>
      </span>
    </el-dialog>
    <!--  @close="guanbi3" -->
    <el-dialog
      title="分配权限"
      @close="guanbi3"
      :visible.sync="duihuakuang3"
      width="50%"
    >
      <!-- 内容主体区域 -->
      <el-tree
        :data="fenpeilist"
        show-checkbox
        node-key="id"
        default-expand-all
        :props="treeProps"
        :default-checked-keys="listkey"
        ref="keyref"
      ></el-tree>
      <!-- 底部区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="duihuakuang3 = false">取 消</el-button>
        <el-button type="primary" @click="queding3">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {
  name: "Roles",
  created() {
    this.Roleslistshuju();
  },
  data() {
    return {
      duihuakuang: false,
      usfrom: {},
      Roleslist: [],
      fenpeilist: [],
      duihuakuang2: false,
      addfrom: {},
      duihuakuang3: false,
      quanxianfrom: {},
      treeProps: {
        label: "authName",
        children: "children",
      },
      listkey: [],
      roleId: "",
    };
  },
  methods: {
    async Roleslistshuju() {
      const { data: res } = await this.$http.get("roles");
      if (res.meta.status != 200)
        return this.$message.error("获取权限列表失败");

      this.Roleslist = res.data;
    },
    async showEditDialog(id) {
      const { data: res } = await this.$http.get("roles/" + id);
      if (res.meta.status != 200)
        return this.$message.error("获取权限列表失败");
      this.usfrom = res.data;
      console.log(this.usfrom);
      this.duihuakuang = true;
    },
    guanbi() {
      this.$refs.usfromref.resetFields();
    },
    async queding() {
      console.log(this.usfrom);
      const { data: res } = await this.$http.put(
        "roles/" + this.usfrom.roleId,
        this.usfrom
      );
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.duihuakuang = false;
      this.Roleslistshuju();
    },
    removeUserById(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.removeUserByIds(id);
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async removeUserByIds(id) {
      const { data: res } = await this.$http.delete(`roles/${id}`);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.Roleslistshuju();
    },

    guanbi2() {
      this.$refs.addfromref.resetFields();
    },
    async queding2() {
      const { data: res } = await this.$http.post("roles", this.addfrom);
      if (res.meta.status !== 201) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.Roleslistshuju();
    },
    shanchu3(row, id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(
            `roles/${row.id}/rights/${id}`
          );
          if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
          this.$message.success(res.meta.msg);
          row.children = res.data;
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },
    async fenpei(role) {
      this.roleId = role.id;
      const { data: res } = await this.$http.get("rights/tree");
      /*  console.log(res); */
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.fenpeilist = res.data;
      this.getkey(role, this.listkey);
      this.duihuakuang3 = true;
    },
    getkey(node, arr) {
      if (!node.children) {
        return arr.push(node.id);
      }
      node.children.forEach((item) => this.getkey(item, arr));
    },
    guanbi3() {
      this.listkey = [];
    },
    async queding3() {
      const keys = [
        ...this.$refs.keyref.getCheckedKeys(),
        ...this.$refs.keyref.getHalfCheckedKeys(),
      ];

      const idstr = keys.join(",");
      const { data: res } = await this.$http.post(
        `roles/${this.roleId}/rights`,
        { rids: idstr }
      );
      console.log(res);
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      this.$message.success(res.meta.msg);
      this.duihuakuang3 = false;
    },
  },
};
</script>

<style lang="less" scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbom {
  border-bottom: 1px solid #eee;
}
.juzhong {
  display: flex;
  align-items: center;
}
</style>